<template>
	<div class="edu-page-warp">
		<inner-breadcrumb v-model="pathKey" :path="path" v-show="pathKey!='main'"></inner-breadcrumb>
		<template v-if="pathKey=='main'">
			<condition-card @reset="reset" @search="onSubmit" v-if="isGraduate" label-width="120px">
				<el-row style="height: 50px;">
					<el-col :span="8" v-if="global.isAnyTeacher">
						<el-form-item :label="$t('管理部门')">
							<el-select v-model="queryForm.managerDeptId" style="width: 100%;">
								<el-option
									v-for="item in options.managerDeptIds"
									:key="item.value"
									:label="item.label"
									:value="item.value"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('course.college')">
							<DictSelect
                                v-model="queryForm.college"
                                :data="options.college"
								:plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('label.trainingLevel')">
							<DictSelect
                                v-model="queryForm.trainingLevel"
                                :data="options.trainingLevel"
								:plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('course.courseProperty')">
							<DictSelect
                                v-model="queryForm.nature"
                                :data="options.nature"
								:plzSelect="true"
								@change="changeNatureForm"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('course.courseCategory')">
							<DictSelect
                                v-model="queryForm.label"
                                :data="options.newLabel"
								:plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('course.teachingLanguage')">
							<DictSelect
                                v-model="queryForm.teachingLanguage"
                                :data="options.teachingLanguage"
								:plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('course.enabled')">
							<DictSelect
                                v-model="queryForm.enabled"
                                :data="options.enabled"
								:plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('开课学期')">
								<DictSelect
                                v-model="queryForm.term"
                                :data="options.term"
								:plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('审核状态')">
								<DictSelect
                                v-model="queryForm.status"
                                :data="options.status"
                                :plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
					<el-col :span="8">
						<el-form-item :label="$t('是否跨院系课程')">
								<DictSelect
                                v-model="queryForm.crossCollege"
                                :data="options.enabled"
                                :plzSelect="true"
                            ></DictSelect>
						</el-form-item>
					</el-col>
				</el-row>
			</condition-card>
			<el-card shadow="never">
				<div class="edu-page-title" style="margin-bottom:16px">{{$t("course.courseApprove")}}</div>
				<div class="edu-opertion-bar" style="margin-bottom:16px" v-if="isGraduate">
					<div class="float-left">
						<el-input
							v-model="queryForm.condition"
							:placeholder="$t('course.inputCourseTip')"
							style="width:240px"
							@keyup.enter.native="onSubmit"
						>
							<i
								class="el-icon-search"
								slot="suffix"
								style="margin-top:10px;cursor:pointer"
								@click="onSubmit"
							></i>
						</el-input>
					</div>
				</div>
				<edu-table
					:selection="false"
					:data="courses"
					@page-change="handleCurrentChange"
					:total="queryForm.total_"
					:pageSize="queryForm.pageSize_"
					:current-page="queryForm.pageNum_"
				>
					<edu-table-column :label="$t('course.college')" prop="college" width="200"/>
					<edu-table-column :label="$t('course.courseCode')" prop="code" width="120"/>
					<edu-table-column :label="$t('course.courseName')" width="240">
						<template slot-scope="props">
							{{getNameByLanguage(props.row)}}
						</template>
					</edu-table-column>
					<edu-table-column :label="$t('course.score')" prop="credits" width="65"/>
					<edu-table-column :label="$t('courses.classHour')" prop="period" width="65"/>
					<edu-table-column :label="$t('course.term')" prop="term" />
					<edu-table-column :label="$t('course.courseProperty')" prop="nature"/>
					<edu-table-column :label="$t('是否跨院系课程')" width="130">						
						<template slot-scope="props">{{props.row.crossCollege == null?"":(props.row.crossCollege == 1?'是':'否')}}</template>
					</edu-table-column>
					<edu-table-column :label="$t('message.applyDate')"  width="110">
						<template slot-scope="props">{{formatDate(props.row.applyAt)}}</template>
					</edu-table-column>
					<edu-table-column :label="$t('message.checkDate')" width="110">
						<template slot-scope="props">{{formatDate(props.row.createdAt||props.row.updatedAt)}}</template>
					</edu-table-column>
					<edu-table-column :label="$t('message.status')" width="110">
						<template slot-scope="props">
							<el-tag
								v-if="props.row.status==3"
								type="success"
								size="mini"
							>{{getStatusText(props.row)}}</el-tag>
							<el-tag
								v-if="props.row.status==2"
								type="info"
								size="mini"
							>{{getStatusText(props.row)}}</el-tag>
							<el-tag
								v-if="props.row.status==1"
								type="warning"
								size="mini"
							>{{getStatusText(props.row)}}</el-tag>
							<el-tag
								v-if="props.row.status==0"
								type="warning"
								size="mini"
							>{{getStatusText(props.row)}}</el-tag>
						</template>
					</edu-table-column>
					<edu-table-column :label="$t('message.operating')" width="100" fixed="right">
						<template slot-scope="props">
							<el-button
								type="text"
								@click="showRollDetail(props.row,true)"
								size="mini"
							>{{$t('shortTermApprove.check')}}</el-button>
							<el-button
								v-if="props.row.no&&props.row.status==2"
								type="text"
								size="mini"
								@click="showRollDetail(props.row)"
							>{{$t('message.check')}}</el-button>
						</template>
					</edu-table-column>
				</edu-table>
			</el-card>
		</template>
		<!-- 审批组件 -->
		<CourseApproveDetail v-if="pathKey!='main'" :approveId="approveId" :courseId="courseId" @back="pathKey='main'" :options="options" :isReadOnly="readOnly"/>
	</div>
</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import UTILS from "common/src/utils/utils";
import { getCourseList } from "common/src/api/courseTemplate";
import {  getFacultys } from "common/src/api/resource/course";
import CourseApproveDetail from "./component/CourseApproveDetail";
import mixin from 'common/src/mixins/index';
export default {
	name: "culturePlanApproveList",
	mixins: [mixin],
	components: {
		CourseApproveDetail
	},
	mounted() {
		if (this.global.isAnyTeacher) {
			this.manageDptIds = this.global.currentUser.manageDptIds.filter(val=>val!='1');
			if(this.manageDptIds.length>0){
				this.cachmanagerDeptId=this.manageDptIds[0]
				this.isGraduate = this.manageDptIds[0] != "1";
			}
		}else{
			this.isGraduate = this.global.currentUser.currentManageDptId != "1";
		}
		this.init();
	},
	watch: {
		pathKey(val) {
			if (val == "main") {
				this.onSubmit();
			}
		},
		'queryForm.managerDeptId' (val) {
			this.isGraduate = this.queryForm.managerDeptId != "1"
			this.onSubmit()
		}
	},
	data() {
		return {			
			pathKey: "main",
			path: {
				label: this.$t("courses.courseManagement"),
				key: "belong",
				disable: true,
				children: [
					{
						label: this.$t("courses.courseApprove"),
						key: "main",
						children: [
							{
								label: this.$t("message.Look"),
								key: "check"
							},
							{
								label: this.$t("courses.approve"),
								key: "approve"
							}
						]
					}
				]
			},
			isGraduate: false,
			courseStatus: {
				DISABLE: 0,
				DRAFT: 1,
				CHECKING: 2,
				PUBLISH: 3
			},
			cachmanagerDeptId:"",
			queryForm: {
				teachingLanguage: "",
				enabled: "",
				label: "",
				nature: "",
				isElective: "",
				managerDeptId: "",
				college: "", //学院
				trainingLevel: "", //培养层次
				condition: "", //课程代码或课程名称
				pageSize_: 20,
				pageNum_: 1,
				total_: 0,
				approvalFlag: "1",
				term:"",//开课学期
				status:"",//审核状态
				crossCollege:""//是否跨院系课程
			},
			options: {
				college: [],
				trainingLevel: [],
				formLearning: [],
				nature: [], //课程性质
				label: [], //课程分类
				newLabel: [],
				tableLabel: [],
				crossCollege: [], //是否跨学院（1是0否）
				crossTrainingLevel: [], //是否本研互选（1是0否）
				term: [],
				scoreType: [], //成绩类型
				teachingLanguage: [], //授课类型
				enabled: [], //研究生字段，课程使用状态
				crossTerm: [], //研究生字段，是否跨学期
				dialogLabelId: [], //弹框的课程分类
				assessmentMode: [], //考试方式
				majorList: [],
				teacherList: [],
				departments: [], //部门
				teachModeList: [], //教学方式
				isElective: [],
				subjectMajorCode:[],//学科专业委员会
				appointUserCode:[],//指定审核人
				managerDeptIds:[],
				status:[
					{label:"待审核",value:21},
					{label:"待教学院长审核",value:22},
					{label:"待学校审核",value:23},
					{label:"已通过",value:3},
					{label:"已驳回",value:0}
				]
			},
			courses: [],
			approveId:null,
			courseId:null,
			manageDptIds:[],
			readOnly:false
		};
	},
	methods: {
		init() {
			let yes = {
					label: this.$t("message.yes"),
					value: 1
				},
				no = {
					label: this.$t("message.no"),
					value: 0
				};
			this.options.isElective.push(yes);
			this.options.isElective.push(no);
			this.options.crossCollege.push(yes);
			this.options.crossCollege.push(no);
			this.options.crossTrainingLevel.push(yes);
			this.options.crossTrainingLevel.push(no);
			this.options.enabled.push(yes);
			this.options.enabled.push(no);
			this.options.crossTerm.push(yes);
			this.options.crossTerm.push(no);
			let data = {
				lang: this.$store.getters.language || "cn",
				type: "allChild",
				keys: [
					"X_PYCC",
					// "X_YX",
					"X_KCXZ",
					"X_KCFL",
					"X_XXXS",
					"X_CJJL",
					"X_SKYY",
					"X_KKXQ",
					"X_KSLX",
					"G_ZY",
					"X_JXFS",
					"X_GLBM"
				]
			};
			queryDic(data).then(res => {
				if (res.code == 200) {
					UTILS.fillSelect(
						{
							trainingLevel: "X_PYCC",
							// college: "X_YX",
							formLearning: "X_XXXS",
							scoreType: "X_CJJL",
							teachingLanguage: "X_SKYY",
							term: "X_KKXQ",
							assessmentMode: "X_KSLX",
							majorList: "G_ZY",
							nature: "X_KCXZ",
							label: "X_KCFL",
							teachModeList: "X_JXFS",
							managerDeptIds:"X_GLBM"
						},
						res.data,
						this.options
					);
					if(this.global.isAnyTeacher){					
						this.options.managerDeptIds=this.options.managerDeptIds.filter(dept=>{
							return this.manageDptIds.includes(dept.value)
						})
						this.options.trainingLevel = this.options.trainingLevel.filter(tr =>{
							return tr.value == '4' || tr.value == '6'
						})
					}
				}
				this.$nextTick(re =>{
					this.queryForm.managerDeptId = this.cachmanagerDeptId
					this.onSubmit();
				})
			});
			//查询开课学院
			getFacultys(this.isGraduate).then(res => {
				if (res.code == 200) {
					let depts =res.data.filter(c => c.deptCode != "-10000");
					this.options.college = depts;
					this.options.college.forEach(c => {
						c.value = c.deptCode;
						c.label =
							"en" == this.$store.getters.language
								? c.deptNameEn
								: c.deptName;
					});
				}
			});			
		},
		onSubmit() {
			if (this.isGraduate) {
				this.getApproveList();
			} else {
				this.queryForm.total_ = 0;
				this.courses = [];
			}
		},
		getApproveList() {
			getCourseList(this.queryForm).then(response => {
				if (response.code == 200) {
					this.courses = response.data.list;
					this.queryForm.total_ = response.data.total_;
				}
			});
		},
		handleCurrentChange(val) {
			this.queryForm.pageSize_ = val.pageSize;
			this.queryForm.pageNum_ = val.pageNum;
			this.getApproveList();
		},
		showRollDetail(row,readOnly=false) {
			if(!readOnly){//审核
				// condition=1的数据，点击审核按钮时看下右上角是教务员还是教师，如果是教务员则弹框提示：请切换到教师身份再审核
				if(row.condition == '1' && this.global.isAcdemicDean){
					this.$alert('当前您登录的身份是"教务员"，请切换到"教师"身份再审核','提示')
					return false
				}
				// condition=2，3的数据，点击审核按钮时看下右上角是教务员还是教师，如果是教师则弹框提示：请切换到教务员身份再审核
				if((row.condition == '2' || row.condition == '3') && this.global.isAnyTeacher){
					this.$alert('当前您登录的身份是"教师"，请切换到"教务员"身份再审核','提示')
					
					return false
				}
			}
			this.readOnly=readOnly;
			this.approveId=row.no;
			this.courseId=row.id;
			this.pathKey = readOnly?"check":"approve";
		},
		reset() {
			this.queryForm.isElective = "";
			this.queryForm.college = "";
			this.queryForm.trainingLevel = "";
			this.queryForm.condition = "";
			this.queryForm.nature = "";
			this.queryForm.label = "";
			this.queryForm.enabled = "";
			this.queryForm.teachingLanguage = "";
			this.options.newLabel.length = 0;
			this.queryForm.term ="";
			this.queryForm.status = "";
			this.queryForm.crossCollege =""
			this.onSubmit();
		},
		getNameByLanguage(obj) {
			let lang = this.$store.getters.language || "cn";
			if (lang == "cn") {
				return obj.name;
			} else {
				return obj.nameEn;
			}
		},
		formatDate(timenumber) {
			return timenumber ? moment(timenumber).format("YYYY-MM-DD") : "";
		},
		getStatusText(row) {
			let val=row.status;
			if (val == this.courseStatus.DISABLE)
				return this.$t("message.reject"); //驳回
			if (val == this.courseStatus.DRAFT)
				return this.$t("message.toBeSubmitted");			
			if (val == this.courseStatus.PUBLISH)
				return this.$t("message.pass");
			if (val == this.courseStatus.CHECKING){
				let obj = this.options.status.find(opt => opt.value == ("2"+row.condition))
				return obj?obj.label:this.$t("message.checkPending");
			}		
			return "";
		},
		changeNatureForm(val) {
			this.options.newLabel = [];
			this.queryForm.label = "";
			let obj = [];
			let labels = this.options.label;
			labels.forEach(item => {
				if (val == "1") {
					if (
						item.value == "01" ||
						item.value == "02" ||
						item.value == "03" ||
						item.value == "07"
					) {
						obj.push({ label: item.label, value: item.value });
					}
				} else if (val == "2") {
					if (
						item.value == "04" ||
						item.value == "05" ||
						item.value == "06" ||
						item.value == "07"
					) {
						obj.push({ label: item.label, value: item.value });
					}
				} else {
					if (item.value == "08" || item.value == "09") {
						obj.push({ label: item.label, value: item.value });
					}
				}
			});
			this.options.newLabel = obj;
		}		
	}
};
</script>
